package com.zhisheng.day16_echarts.controller;

import com.zhisheng.day16_echarts.dto.StudentScoreDto;
import com.zhisheng.day16_echarts.entity.StudentScore;
import com.zhisheng.day16_echarts.service.StudentScoreService;
import com.zhisheng.day16_echarts.vo.MyEchartsVo;
import com.zhisheng.day16_echarts.vo.R;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * (StudentScore)表控制层
 *
 * @author 全哥哥
 * @since 2025-04-30 14:56:21
 */
@RestController
@RequestMapping("studentScore")
public class StudentScoreController {
    /**
     * 服务对象
     */
    @Resource
    private StudentScoreService studentScoreService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<StudentScore> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.studentScoreService.queryById(id));
    }

    @GetMapping("avg")
    public ResponseEntity<Map> queryAvgListByName() {
        return ResponseEntity.ok(this.studentScoreService.findAvgByGroupName());
    }


    /**
     * 新增数据
     *
     * @param studentScore 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<StudentScore> add(StudentScore studentScore) {
        return ResponseEntity.ok(this.studentScoreService.insert(studentScore));
    }


    @PostMapping("insertByExcel")
    public ResponseEntity<Integer> insertByExcel(MultipartFile excel) throws IOException {
        //  解析,希望得到  List<StudentScore> 调用 mapper 传递集合,即可完成批量添加
        InputStream inputStream = excel.getInputStream();
        int count = studentScoreService.insertByExcel(inputStream);
        return ResponseEntity.ok(count);
    }

    /**
     * 带条件的分页查询
     */
    @PostMapping("/listByPage")
    public R<Map> listByPage(@RequestBody StudentScoreDto dto) {
        Map map = studentScoreService.listByPage(dto);
        return R.success(map);
    }

    /**
     * 带条件的分页查询且结果展示为 图表
     */
    @PostMapping("/listByPageAndEcharts")
    public R<Collection<MyEchartsVo>> listByPageAndEcharts(@RequestBody StudentScoreDto dto) {
        Collection<MyEchartsVo> map = studentScoreService.listByPageAndEcharts(dto);
        return R.success(map);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.studentScoreService.deleteById(id));
    }

}

